package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.ArticleDetailsCommentBean;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class Article_DetailsAdapter extends BaseAdapter{
    List<ArticleDetailsCommentBean.DataEntity> mList;
    LayoutInflater inflater;
    Context context;
    ViewHolder viewHolder;
    OnItemClickListener mOnItemClickListener;

    public Article_DetailsAdapter(List<ArticleDetailsCommentBean.DataEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onZanClick(int position, boolean isFabulous, int commentId);
    }

    public void setList(List<ArticleDetailsCommentBean.DataEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public ArticleDetailsCommentBean.DataEntity getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_article_details_recomment, null);
            viewHolder = new ViewHolder();

            viewHolder.textView_article_details_username = (TextView) convertView.findViewById(R.id.textView_article_details_username);
            viewHolder.textView_article_details_content = (TextView) convertView.findViewById(R.id.textView_article_details_content);
            viewHolder.textView_article_details_zan = (TextView) convertView.findViewById(R.id.textView_article_details_zan);
            viewHolder.imageView_article_details_recomment_usersimg = (SimpleDraweeView) convertView.findViewById(R.id.imageView_article_details_recomment_usersimg);
            viewHolder.linearLayout_article_details_zan = (LinearLayout) convertView.findViewById(R.id.linearLayout_article_details_zan);
            viewHolder.imageView_article_details_zan = (ImageView) convertView.findViewById(R.id.imageView_article_details_zan);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final ArticleDetailsCommentBean.DataEntity dataEntity = getItem(position);
        final boolean isFabulous = dataEntity.isIsFabulous();
        viewHolder.textView_article_details_username.setText(dataEntity.getNickname());
        viewHolder.textView_article_details_content.setText(dataEntity.getContent());
        viewHolder.textView_article_details_zan.setText(dataEntity.getFabulousCount()+"");
        viewHolder.textView_article_details_zan.setTag(R.id.article_details_comment,true);

        if (dataEntity.getAvatar() != null){
            if (dataEntity.getAvatar() != ""){
                viewHolder.imageView_article_details_recomment_usersimg.setImageURI(Uri.parse(dataEntity.getAvatar()));
            }
        }
        if(isFabulous){
            viewHolder.imageView_article_details_zan.setImageResource(R.drawable.icon_dianzan_h);
        }else{
            viewHolder.imageView_article_details_zan.setImageResource(R.drawable.icon_dianzan_n);
        }

        viewHolder.textView_article_details_zan.setText(dataEntity.getFabulousCount()+"");

        viewHolder.linearLayout_article_details_zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onZanClick(position,isFabulous,dataEntity.getCommentId());
            }
        });
        return convertView;
    }

    public class ViewHolder {
        TextView textView_article_details_username;
        TextView textView_article_details_content;
        TextView textView_article_details_zan;
        SimpleDraweeView imageView_article_details_recomment_usersimg;

        ImageView imageView_article_details_zan;

        LinearLayout linearLayout_article_details_zan;
    }

    public List<ArticleDetailsCommentBean.DataEntity> getList(){
        return mList;
    }
}
